package cup.com.icss.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cup.com.icss.dao.DormitoryMapper;
import cup.com.icss.dao.UserMapper;
import cup.com.icss.model.Dormitory;
import cup.com.icss.model.User;
import cup.com.icss.service.UserService;
import cup.com.icss.utils.LogUtil;
import cup.com.icss.utils.StringUtil;
@Controller
public class UserManagementController {
	@Resource
	private UserService userService;
	@Resource
	private UserMapper userMapper;
	@Resource
	private DormitoryMapper dormitoryMapper;
	
	@RequestMapping(value="/UserManagement",method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView UserManagement(HttpServletRequest request,String storiedBuilding){
		ModelAndView view = new ModelAndView();
		try {
			User user = (User)request.getSession().getAttribute("USER");
			view.addObject("studentId",user.getStudentId());
			view.addObject("storiedBuilding",storiedBuilding);
			view.setViewName("cup/jsp/userManagement/UserManagement");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return view;
	}
	
	@RequestMapping(value="/ListForUser",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> ListForUser(HttpServletRequest request,Integer page,Integer rows,User record){
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			page=page==null?1:page;
			rows=rows==null?10:rows;
			//PageHelper.startPage(pageNum, pageSize);
			map = userService.selectUserList(page,rows,record);
			//PageInfo<User> paGe = new PageInfo<User>(user);
			//map.put("total", user.size());
			//map.put("rows", paGe.getList());
			//map.put("pageNum", new Integer(paGe.getPageNum()));//
			//map = ListPage.setPageParamToMap(paGe);
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping("/addUserManagement")
	@ResponseBody
	public ModelAndView showUserManagement(HttpServletRequest request,String storiedBuilding){
		ModelAndView view = new ModelAndView();
		try {
			view.addObject("storiedBuilding",storiedBuilding);
			view.setViewName("cup/jsp/userManagement/UserManagementAdd");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return view;
	}
	
	@RequestMapping(value="/UserManagementAdd",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> UserManagementAdd(HttpServletRequest request,User record,String dormitory){
		Map<String,String> map = new HashMap<String,String>();
		try {
			record.setStudentId(record.getUserName());
			record.setRole("学生");
			record.setRoleName("GENERAL");
			record.setDisable("1");
			User user = userMapper.selectByPrimaryKey(record.getStudentId());
			if(user == null){
				
				Dormitory dor = new Dormitory();
				dor.setId(StringUtil.getUUID());
				dor.setStoriedBuilding(record.getStoriedBuilding());
				dor.setDormitory(dormitory);
				dor.setCnName(record.getCnName());
				dor.setSex(record.getSex());
				dor.setPnoneNumber("-");
				dor.setCollege(record.getCollege());
				dor.setTheClass(record.getTheClass());
				dor.setStudentId(record.getStudentId());
				int inster2 = dormitoryMapper.insertSelective(dor);
				
				int inster = userMapper.insert(record);
				if(inster==1 && inster2==1){
					map.put("state", "200");
				}else{
					map.put("state", "400");
				}
			}else{
				map.put("state", "500");
			}			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return map;
	}
	
	@RequestMapping(value="/editUserManagement",method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView editUserManagement(HttpServletRequest request,String studentId){
		ModelAndView view = new ModelAndView();
		try {
			User user = userMapper.selectByPrimaryKey(studentId);
			view.addObject("user",user);
			view.setViewName("cup/jsp/userManagement/UserManagementEdit");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return view;
	}
	
	@RequestMapping(value="/UserManagementEdit",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> UserManagementEdit(HttpServletRequest request,User record){
		Map<String,String> map = new HashMap<String,String>();
		try {
			int upda = userMapper.updateByPrimaryKeySelective(record);
			if(upda>0){
				map.put("state", "200");
			}else{
				map.put("state", "500");
			}			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return map;
	}
	
	@RequestMapping(value="/UserManagementDelete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> UserManagementDelete(HttpServletRequest request,String studentId){
		Map<String, String> map = new HashMap<String, String>();
		try {
			if(studentId!=""){
				int delete = userMapper.deleteByPrimaryKey(studentId);
				int del = dormitoryMapper.deleteByStudentId(studentId);
				if(delete>0){
					map.put("state", "200");
				}else{
					map.put("state", "500");
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping("/mailSystemInitialize")
	@ResponseBody
	public ModelAndView mailSystemInitialize(HttpServletRequest request){
		ModelAndView view = new ModelAndView();
		try {
			User user = (User)request.getSession().getAttribute("USER");
			view.addObject("studentId",user.getStudentId());
			view.addObject("cnName",user.getCnName());
			view.addObject("college",user.getCollege());
			view.addObject("theClass",user.getTheClass());
			view.setViewName("cup/jsp/mailSystem/mailSystemAddInitialize");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	@RequestMapping(value="/peopleInformation",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> peopleInformation(HttpServletRequest request, Integer rows, Integer page, User record){
		Map<String, Object> map = new HashMap<String,Object>();
		try {
			page=page==null?1:page;
			rows=rows==null?10:rows;
			int pageNum = page;
			int pageSize = rows;
			int num = userMapper.peopleInformationSize(record).size();
			int pageSizez;
			if(((pageSize*pageNum)-num)<0){
				pageSizez = pageSize;
			}else{
				pageSizez = num-(10*(pageNum-1));
			}
			List<User> somethingLost = userMapper.peopleInformation(pageNum*pageSize-pageSize,pageSizez,record);
			map.put("total", num);
			map.put("rows", somethingLost);
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
}
